<?php

namespace app\admin\controller;

use app\admin\validate\Admin as AdminValidate;
use app\admin\model\Admin as AdminModel;
use think\App;

class Admin extends Base
{

    protected $admin = '';

    public function __construct(App $app, AdminModel $admin)
    {
        parent::__construct($app);
        $this->admin = $admin;
    }

    public function index(): string
    {
        if (isAjax()) {
            $postData = input('request.');

            $where = [['status', '>=', 0], ['id', '>', 1]];
            !empty($postData['keyword']) && $where['name|true_name'] = ['like', '%' . $postData['keyword'] . '%'];
            !empty($postData['auth_id']) && $where['auth_id'] = $postData['auth_id'];
            $result = $this->admin->withoutField('password')->where($where)->order('id', 'desc')->paginate(10);
            ajaxSuccess($result);
        }
        return $this->fetch();
    }

    public function saveAdmin(AdminValidate $adminValidate)
    {
        if (isAjax()) {
            $postData = input('post.');
            $valid = $adminValidate->check($postData);
            if (!$valid) {
                ajaxError($adminValidate->getError());
            }
            if (isset($postData['password'])) $postData['password'] = createPwd($postData['password']);
            unset($postData['create_time'], $postData['view_cover']);
            if (isset($postData['id'])) {
                $exits = $this->admin->where('id', '<>', $postData['id'])->where('name', $postData['name'])->where('status', 1)->count();
                if ($exits) ajaxError('账号已存在');
                $result = $this->admin->update($postData);
            } else {
                $exits = $this->admin->where('name', $postData['name'])->where('status', 1)->count();
                if ($exits) ajaxError('账号已存在');
                $result = $this->admin->save($postData);
            }
            if (!$result)
                ajaxError('保存失败');
            ajaxSuccess();
        }
    }

    public function delAdmin()
    {
        if (isAjax()) {
            $id = input('post.id');
            $result = $this->admin->update(['status' => -1], ['id' => $id]);
            if ($result) {
                ajaxSuccess();
            }
        }
        ajaxError();
    }

    public function updatePwd()
    {
        if (isAjax()) {
            $old = input('post.old');
            $new = input('post.new');
            if (empty($old) || empty($new)) {
                ajaxError('请输入原密码或新密码');
            }
            $len = mb_strlen($new);
            if ($len < 6 || $len > 20) {
                ajaxError('密码长度为6-20位');
            }
            $admin = $this->admin->where(['id' => $this->userId])->find();
            if ($admin['password'] != createPwd($old)) {
                ajaxError('原密码不正确');
            }
            $result = $this->admin->update(['password' => createPwd($new), 'id' => $this->userId]);
            if ($result) {
                ajaxSuccess();
            }
        }
        ajaxError();
    }

}

